package com.sskj.hangqing.ui.activity;


import android.support.v4.app.Fragment;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.common.util.ClickUtil;
import com.sskj.hangqing.R;
import com.sskj.hangqing.R2;
import com.sskj.hangqing.presenter.MarketActivityPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.BibiCoinType;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.LevelCoinType;
import com.sskj.lib.bean.TabItem;
import com.sskj.lib.bean.enums.ColorEnum;
import com.sskj.lib.box.LiveDataBus;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;

@Route(path = RConfig.HANG_MARKET)
public class MarketActivity extends BaseActivity<MarketActivityPresenter> {
    @Autowired
    String code;
    @Autowired
    String qu;
    @BindView(R2.id.frameLayoutChart)
    FrameLayout frameLayoutChart;
    @BindView(R2.id.frameLayoutBottom)
    FrameLayout frameLayoutBottom;
    @BindView(R2.id.slidingTabLayout)
    CommonTabLayout slidingTabLayout;
    @BindView(R2.id.btTrade)
    Button btTrade;
    @BindView(R2.id.tvUSDT)
    TextView tvUSDT;
    @BindView(R2.id.tvHigh)
    TextView tvHigh;
    @BindView(R2.id.tvRMB)
    TextView tvRMB;
    @BindView(R2.id.tvRate)
    TextView tvRate;
    @BindView(R2.id.tvLow)
    TextView tvLow;
    @BindView(R2.id.tvTitleName)
    TextView tvTitleName;
    @Autowired
    boolean isBibi;

    private HashMap<Integer, Fragment> bottomMap;
    private int curPos;


    @Override
    protected int getLayoutId() {
        return R.layout.hang_activity_market_scroll;
    }

    @Override
    public MarketActivityPresenter getPresenter() {
        return new MarketActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        setTitle(isBibi ? getString(R.string.hangqing_marketActivity1) : getString(R.string.hangqinghang_activity_market_scroll40));
        tvTitleName.setText(code);
        btTrade.setText(isBibi ? getString(R.string.hangqingguideWebActivity2) : getString(R.string.hangqinghang_activity_market_scroll700));
        ClickUtil.click(btTrade, () -> {
            if (isBibi) {
                LiveDataBus.get().with(RxBusCode.BIBI_CHANGE_COIN, BibiCoinType.class).postValue(new BibiCoinType(code));
                LiveDataBus.get().with(RxBusCode.CHANGE_MAIN_TAB, Integer.class)
                        .postValue(3);
            } else {
                LiveDataBus.get().with(RxBusCode.LEVEL_CHANGE_COIN, LevelCoinType.class).postValue(new LevelCoinType(code));
                LiveDataBus.get().with(RxBusCode.CHANGE_MAIN_TAB, Integer.class)
                        .postValue(1);
            }
            finish();
        });
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.frameLayoutChart, (Fragment) ARouter.getInstance()
                        .build(RConfig.HANG_FRAGMENT_CHART)
                        .withString(Constans.CODE, code)
                        .withBoolean(Constans.IS_BIBI, isBibi)
                        .withString(Constans.GOOD_TYPE, "minute")
                        .withBoolean(Constans.IS_CANDLE, false)
                        .navigation()
                )
                .commitAllowingStateLoss();

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add((Fragment) ARouter.getInstance()
                .build(RConfig.HANG_FRAGMENT_DEEPTH)
                .withString(Constans.CODE, code)
                .withBoolean(Constans.IS_BIBI, isBibi)
                .navigation());
        fragments.add((Fragment) ARouter.getInstance()
                .build(RConfig.HANG_FRAGMENT_TRADE)
                .withString(Constans.CODE, code)
                .withBoolean(Constans.IS_BIBI, isBibi)
                .navigation());
        fragments.add((Fragment) ARouter.getInstance()
                .build(RConfig.HANG_FRAGMENT_SUMMARY)
                .withString(Constans.CODE, code)
                .navigation());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.frameLayoutBottom, fragments.get(0))
                .show(fragments.get(0))
                .commitAllowingStateLoss();
        bottomMap = new HashMap<>();
        bottomMap.put(0, fragments.get(0));
        curPos = 0;
        ArrayList<CustomTabEntity> tabItems = new ArrayList<>();
        tabItems.add(new TabItem(getString(R.string.hangqingmarketActivity1), 0, 0));
        tabItems.add(new TabItem(getString(R.string.hangqingmarketActivity2), 0, 0));
        tabItems.add(new TabItem(getString(R.string.hangqingmarketActivity3), 0, 0));
        slidingTabLayout.setTabData(tabItems);
        slidingTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (bottomMap.get(position) == null) {
                    getSupportFragmentManager().beginTransaction()
                            .add(R.id.frameLayoutBottom, fragments.get(position))
                            .hide(fragments.get(curPos))
                            .show(fragments.get(position))
                            .commitAllowingStateLoss();
                    bottomMap.put(position, fragments.get(position));
                } else {
                    getSupportFragmentManager().beginTransaction()
                            .hide(fragments.get(curPos))
                            .show(fragments.get(position))
                            .commitAllowingStateLoss();
                }
                curPos = position;
            }

            @Override
            public void onTabReselect(int position) {

            }
        });


    }

    @Override
    protected void initData() {
        mPresenter.getData(code);
        LiveDataBus.get().with(isBibi ? RxBusCode.PUSH_COIN_BEAN_BIBI : RxBusCode.PUSH_COIN_BEAN_LEVEL, CoinBean.class)
                .observe(this, this::refreshCoin);
    }


    public void refreshCoin(CoinBean coinBean) {
        if (coinBean.getCode().equals(code)) {
            tvUSDT.setText(String.format("%s%s", coinBean.getPrice(), coinBean.getRCode()));
            tvRMB.setText(String.format("≈%s " + "CNY", coinBean.getCnyPrice()));
            tvRate.setText(coinBean.getChangeRate());
            tvLow.setText(coinBean.getLow());
            tvHigh.setText(coinBean.getHigh());
            tvUSDT.setTextColor(ColorEnum.getColor(coinBean.isUp()));
            tvRate.setTextColor(ColorEnum.getColor(coinBean.isUp()));
        }
    }
}

